From 328c9d654a4a0bf304e6a1785c3a63f200c0bb64 Mon Sep 17 00:00:00 2001 From: tsteven4 Date: Wed, 28 Nov 2018 07:00:10 -0700 Subject: [PATCH] use QThread::usleep instead of our own function. (#277) Our test suite doesn't test sleep, but the nmea pause option can be used on write to verify functionality interactively. --- CMakeLists.txt | 4 ++-- GPSBabel.pro | 4 ++-- Makefile.in | 3 +-- config.h.in | 10 ++------- configure | 2 +- configure.ac | 2 +- defs.h | 5 ----- garmin_gpi.cc | 3 ++- gbsleep.cc | 53 ---------------------------------------------- jeeps/gpsserial.cc | 35 +++++++++++++++--------------- mtk_logger.cc | 11 +++++----- navilink.cc | 5 +++-- nmea.cc | 9 ++++---- skytraq.cc | 5 +++-- 14 files changed, 46 insertions(+), 105 deletions(-) delete mode 100644 gbsleep.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 111fda093..cd824e6a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,7 +94,7 @@ set(SUPPORT queue.cc route.cc waypt.cc filter_vecs.cc util.cc vecs.cc mkshort.cc csv_util.cc strptime.c grtcirc.cc util_crc.cc xmlgeneric.cc formspec.cc xmltag.cc cet.cc cet_util.cc fatal.cc rgbcolors.cc - inifile.cc garmin_fs.cc gbsleep.cc units.cc gbser.cc + inifile.cc garmin_fs.cc units.cc gbser.cc gbfile.cc parse.cc session.cc main.cc globals.cc src/core/usasciicodec.cc src/core/xmlstreamwriter.cc @@ -186,7 +186,7 @@ if(UNIX) if(${HAVE_STDARG_H}) add_definitions(-DHAVE_STDARG_H) endif() - add_definitions(-DHAVE_NANOSLEEP -DHAVE_LIBUSB -DHAVE_GLOB) + add_definitions(-DHAVE_LIBUSB -DHAVE_GLOB) set(SOURCES ${SOURCES} gbser_posix.cc) set(HEADERS ${HEADERS} gbser_posix.h) set(JEEPS ${JEEPS} jeeps/gpslibusb.cc) diff --git a/GPSBabel.pro b/GPSBabel.pro index ecf10d070..a5adff197 100644 --- a/GPSBabel.pro +++ b/GPSBabel.pro @@ -79,7 +79,7 @@ JEEPS += jeeps/gpsapp.cc jeeps/gpscom.cc \ SUPPORT = queue.cc route.cc waypt.cc filter_vecs.cc util.cc vecs.cc mkshort.cc \ csv_util.cc strptime.c grtcirc.cc util_crc.cc xmlgeneric.cc \ formspec.cc xmltag.cc cet.cc cet_util.cc fatal.cc rgbcolors.cc \ - inifile.cc garmin_fs.cc gbsleep.cc units.cc gbser.cc \ + inifile.cc garmin_fs.cc units.cc gbser.cc \ gbfile.cc parse.cc session.cc main.cc globals.cc \ src/core/usasciicodec.cc \ src/core/xmlstreamwriter.cc @@ -169,7 +169,7 @@ macx|linux { # this is used by zlib DEFINES += HAVE_STDARG_H } - DEFINES += HAVE_NANOSLEEP HAVE_LIBUSB HAVE_GLOB + DEFINES += HAVE_LIBUSB HAVE_GLOB SOURCES += gbser_posix.cc HEADERS += gbser_posix.h JEEPS += jeeps/gpslibusb.cc diff --git a/Makefile.in b/Makefile.in index d4dbfcf47..ff2126c1f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -119,7 +119,7 @@ ZLIB=zlib/adler32.o zlib/compress.o zlib/crc32.o zlib/deflate.o zlib/inffast.o \ LIBOBJS = queue.o route.o waypt.o filter_vecs.o util.o vecs.o mkshort.o \ csv_util.o strptime.o grtcirc.o util_crc.o xmlgeneric.o \ formspec.o xmltag.o cet.o cet_util.o fatal.o rgbcolors.o \ - inifile.o garmin_fs.o gbsleep.o units.o @GBSER@ gbser.o \ + inifile.o garmin_fs.o units.o @GBSER@ gbser.o \ gbfile.o parse.o session.o \ src/core/xmlstreamwriter.o \ src/core/usasciicodec.o\ @@ -587,7 +587,6 @@ gbser.o: gbser.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h \ gbser_posix.o: gbser_posix.cc defs.h config.h queue.h zlib/zlib.h \ zlib/zconf.h gbfile.h cet.h inifile.h session.h src/core/datetime.h \ src/core/optional.h gbser.h gbser_private.h -gbsleep.o: gbsleep.cc config.h gdb.o: gdb.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h gbfile.h \ cet.h inifile.h session.h src/core/datetime.h src/core/optional.h \ cet_util.h csv_util.h garmin_fs.h jeeps/gps.h jeeps/../defs.h \ diff --git a/config.h.in b/config.h.in index f553d8520..9e84d0f3a 100644 --- a/config.h.in +++ b/config.h.in @@ -9,8 +9,8 @@ /* 1 to enable all the filters. */ #undef FILTERS_ENABLED -/* define if the compiler supports basic C++11 syntax */ -#undef HAVE_CXX11 +/* define if the compiler supports basic C++14 syntax */ +#undef HAVE_CXX14 /* Define to 1 if you have the `glob' function. */ #undef HAVE_GLOB @@ -36,12 +36,6 @@ /* Define to 1 if you have the header file. */ #undef HAVE_MEMORY_H -/* Define to 1 if you have the `nanosleep' function. */ -#undef HAVE_NANOSLEEP - -/* Define to 1 if you have the `sleep' function. */ -#undef HAVE_SLEEP - /* Define to 1 if you have the header file. */ #undef HAVE_STDARG_H diff --git a/configure b/configure index b03f035e3..d857f9dd4 100755 --- a/configure +++ b/configure @@ -6751,7 +6751,7 @@ done # AC_FUNC_STRTOD # AC_FUNC_VPRINTF # AC_CHECK_FUNCS([atexit floor localtime_r memmove memset pow select sqrt strchr strcspn strdup strerror strncasecmp strrchr strspn strstr strtol strtoul]) -for ac_func in nanosleep sleep uname glob +for ac_func in uname glob do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_cxx_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/configure.ac b/configure.ac index 801514164..4e0d38716 100644 --- a/configure.ac +++ b/configure.ac @@ -426,7 +426,7 @@ AC_CHECK_HEADERS([unistd.h stdarg.h]) # AC_FUNC_STRTOD # AC_FUNC_VPRINTF # AC_CHECK_FUNCS([atexit floor localtime_r memmove memset pow select sqrt strchr strcspn strdup strerror strncasecmp strrchr strspn strstr strtol strtoul]) -AC_CHECK_FUNCS([nanosleep sleep uname glob]) +AC_CHECK_FUNCS([uname glob]) # # There's probably some more "autoconfish" way to do this. diff --git a/defs.h b/defs.h index b4aa31267..068faf289 100644 --- a/defs.h +++ b/defs.h @@ -1186,11 +1186,6 @@ double fmt_distance(const double, const char** tag); double fmt_altitude(const double, const char** tag); double fmt_speed(const double, const char** tag); -/* - * From gbsleep.c - */ -void gb_sleep(unsigned long microseconds); - /* * From nmea.c */ diff --git a/garmin_gpi.cc b/garmin_gpi.cc index f60092a04..028c85c69 100644 --- a/garmin_gpi.cc +++ b/garmin_gpi.cc @@ -50,6 +50,7 @@ #include "garmin_gpi.h" #include "jeeps/gpsmath.h" #include +#include #include #define MYNAME "garmin_gpi" @@ -1573,7 +1574,7 @@ garmin_gpi_wr_deinit() } gpi_timestamp += sleep; while (gpi_timestamp > time(nullptr)) { - gb_sleep(100); + QThread::usleep(100); } } } diff --git a/gbsleep.cc b/gbsleep.cc deleted file mode 100644 index 25da77f2d..000000000 --- a/gbsleep.cc +++ /dev/null @@ -1,53 +0,0 @@ -/* - OS abstraction to sleep a given number of milliseconds. - - Copyright (C) 2006 Robert Lipe, robertlipe+source@gpsbabel.org - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA - - */ -#if HAVE_CONFIG_H -#include "config.h" -#endif - -#if __WIN32__ - -#include -void -gb_sleep(unsigned long microseconds) -{ - Sleep(microseconds/1000 + 1); -} - -#elif defined HAVE_NANOSLEEP - -#include -void -gb_sleep(unsigned long microseconds) -{ - struct timespec req; - req.tv_sec = microseconds / 1000000; - req.tv_nsec = (microseconds * 1000) % 1000000000; - nanosleep(&req, nullptr); -} -#elif defined HAVE_SLEEP -/* Amazingly underachieving, but probably "good enough" */ -#include -void -gb_sleep(unsigned long microseconds) -{ - sleep(microseconds / 1000000); -} -#endif diff --git a/jeeps/gpsserial.cc b/jeeps/gpsserial.cc index 2ccd1f302..26343a89b 100644 --- a/jeeps/gpsserial.cc +++ b/jeeps/gpsserial.cc @@ -26,6 +26,7 @@ #include "gps.h" #include "../gbser.h" #include "gpsserial.h" +#include #include #include #include @@ -235,7 +236,7 @@ int32 GPS_Serial_Set_Baud_Rate(gpsdevh* fd, int br) GPS_PPacket tra; GPS_PPacket rec; win_serial_data* wsd = (win_serial_data*)fd; - + DWORD speed = mkspeed(br); // Turn off all requests by transmitting packet @@ -247,7 +248,7 @@ int32 GPS_Serial_Set_Baud_Rate(gpsdevh* fd, int br) if (!GPS_Get_Ack(fd, &tra, &rec)) { return gps_errno; } - + GPS_Util_Put_Int(data, br); GPS_Make_Packet(&tra, 0x30, data, 4); if (!GPS_Write_Packet(fd,tra)) { @@ -268,11 +269,11 @@ int32 GPS_Serial_Set_Baud_Rate(gpsdevh* fd, int br) } GPS_Device_Flush(fd); GPS_Device_Wait(fd); - + // Sleep for a small amount of time, about 100 milliseconds, // to make sure the packet was successfully transmitted to the GPS unit. - gb_sleep(100000); - + QThread::usleep(100000); + // Change port speed DCB tio; tio.DCBlength = sizeof(DCB); @@ -302,10 +303,10 @@ int32 GPS_Serial_Set_Baud_Rate(gpsdevh* fd, int br) if (!GPS_Get_Ack(fd, &tra, &rec)) { return gps_errno; } - + if (global_opts.debug_level >= 1) fprintf(stderr, "Serial port speed set to %d\n", br); return 0; - + } #else @@ -623,7 +624,7 @@ int32 GPS_Serial_Set_Baud_Rate(gpsdevh* fd, int br) static UC data[4]; GPS_PPacket tra; GPS_PPacket rec; - + speed_t speed = mkspeed(br); // Turn off all requests by transmitting packet @@ -635,7 +636,7 @@ int32 GPS_Serial_Set_Baud_Rate(gpsdevh* fd, int br) if (!GPS_Get_Ack(fd, &tra, &rec)) { return gps_errno; } - + GPS_Util_Put_Int(data, br); GPS_Make_Packet(&tra, 0x30, data, 4); if (!GPS_Write_Packet(fd,tra)) { @@ -656,23 +657,23 @@ int32 GPS_Serial_Set_Baud_Rate(gpsdevh* fd, int br) } GPS_Device_Flush(fd); GPS_Device_Wait(fd); - + // Sleep for a small amount of time, about 100 milliseconds, // to make sure the packet was successfully transmitted to the GPS unit. - gb_sleep(100000); - + QThread::usleep(100000); + // Change port speed posix_serial_data* psd = (posix_serial_data*)fd; tty = psd->gps_ttysave; - + cfsetospeed(&tty,speed); cfsetispeed(&tty,speed); - + if (tcsetattr(psd->fd,TCSANOW|TCSAFLUSH,&tty)==-1) { GPS_Serial_Error("SERIAL: tcsetattr error"); return 0; } - + GPS_Util_Put_Short(data, 0x3a); GPS_Make_Packet(&tra, 0x0a, data, 2); if (!GPS_Write_Packet(fd,tra)) { @@ -690,10 +691,10 @@ int32 GPS_Serial_Set_Baud_Rate(gpsdevh* fd, int br) if (!GPS_Get_Ack(fd, &tra, &rec)) { return gps_errno; } - + if (global_opts.debug_level >= 1) fprintf(stderr, "Serial port speed set to %d\n", br); return 0; - + } #endif /* __WIN32__ */ diff --git a/mtk_logger.cc b/mtk_logger.cc index 8095c09ff..00b96396d 100644 --- a/mtk_logger.cc +++ b/mtk_logger.cc @@ -59,6 +59,7 @@ #include "gbser.h" #include #include +#include #include #include #include @@ -517,11 +518,11 @@ static int mtk_erase() dbg(1, "Start flash erase..\n"); do_cmd(CMD_LOG_DISABLE, "PMTK001,182,5,3", nullptr, 1); - gb_sleep(10*1000); + QThread::usleep(10 * 1000); // Erase log.... do_cmd(CMD_LOG_ERASE, "PMTK001,182,6", nullptr, 30); - gb_sleep(100*1000); + QThread::usleep(100 * 1000); if ((log_status & 2)) { // auto-log were enabled before..re-enable log. int err = do_cmd(CMD_LOG_ENABLE, "PMTK001,182,4,3", nullptr, 2); @@ -576,12 +577,12 @@ static void mtk_read() fusage = nullptr; } - gb_sleep(10*1000); + QThread::usleep(10 * 1000); if (true || log_enabled) { i = do_cmd(CMD_LOG_DISABLE, "PMTK001,182,5,3", nullptr, 2); dbg(3, " ---- LOG DISABLE ---- %s\n", i==0?"Success":"Fail"); } - gb_sleep(100*1000); + QThread::usleep(100 * 1000); unsigned int addr_max = 0; // get flash usage, current log address..cmd only works if log disabled. @@ -716,7 +717,7 @@ mtk_retry: if (line[15] != '3') { // fixme - we should timeout here when no log data has been received... dbg(2, "\nLog req. failed (%c)\n", line[15]); - gb_sleep(10*1000); + QThread::usleep(10 * 1000); retry_cnt++; goto mtk_retry; } diff --git a/navilink.cc b/navilink.cc index cc3b7f165..229044af0 100644 --- a/navilink.cc +++ b/navilink.cc @@ -26,6 +26,7 @@ #include "gbser.h" #include "jeeps/gpsmath.h" #include "navilink.h" +#include #define MYNAME "NAVILINK" @@ -598,7 +599,7 @@ serial_write_track() data[6] = 0x00; write_packet(PID_WRITE_TRACKPOINTS, data, sizeof(data)); - gb_sleep(10000); + QThread::usleep(10000); write_packet(PID_DATA, track_data, track_data_ptr - track_data); read_packet(PID_CMD_OK, nullptr, 0, 0, false); @@ -1045,7 +1046,7 @@ nuke() * takes around 1 second. The total sectors for SBP is 10. * So give the device some time to clear its datalog, in addition * to SERIAL_TIMEOUT, which applies to read_packet() */ - gb_sleep(CLEAR_DATALOG_TIME * 1000); + QThread::usleep(CLEAR_DATALOG_TIME * 1000); read_packet(PID_ACK, nullptr, 0, 0, false); } } diff --git a/nmea.cc b/nmea.cc index f73deb26f..00b26e63c 100644 --- a/nmea.cc +++ b/nmea.cc @@ -35,6 +35,7 @@ #include #include +#include /********************************************************** @@ -1229,7 +1230,7 @@ nmea_wayptpr(const Waypoint* wpt) gbfprintf(file_out, "$%s*%02X\n", obuf, cksum); if (sleepus >= 0) { gbfflush(file_out); - gb_sleep(sleepus); + QThread::usleep(sleepus); } } static void @@ -1251,11 +1252,11 @@ nmea_trackpt_pr(const Waypoint* wpt) gbfflush(file_out); if (last_time > 0) { if (sleepus >= 0) { - gb_sleep(sleepus); + QThread::usleep(sleepus); } else { long wait_time = wpt->GetCreationTime().toTime_t() - last_time; if (wait_time > 0) { - gb_sleep(wait_time * 1000000); + QThread::usleep(wait_time * 1000000); } } } @@ -1454,6 +1455,6 @@ void reset_sirf_to_nmea(int br) pkt[27] = br & 0xff; sirf_write(pkt); - gb_sleep(250 * 1000); + QThread::usleep(250 * 1000); gbser_flush(gbser_handle); } diff --git a/skytraq.cc b/skytraq.cc index b712a9d60..5bbed2294 100644 --- a/skytraq.cc +++ b/skytraq.cc @@ -25,6 +25,7 @@ #include "defs.h" #include "gbser.h" +#include #include #include #include @@ -469,7 +470,7 @@ skytraq_set_baud(int baud) return res_ERROR; } - gb_sleep(50); /* allow UART to settle. */ + QThread::usleep(50); /* allow UART to settle. */ return res_OK; } @@ -1206,7 +1207,7 @@ skytraq_probe() } } - gb_sleep(50); /* allow UART to settle. */ + QThread::usleep(50); /* allow UART to settle. */ skytraq_wr_msg(MSG_QUERY_SOFTWARE_VERSION, /* get firmware version */ sizeof(MSG_QUERY_SOFTWARE_VERSION)); -- 2.30.2